package cn.guozeyiblog.client;

import java.util.ArrayList;

import cn.guozeyiblog.dbhelper.DBHelper;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class CityListActivity extends ListActivity implements OnItemClickListener {
	
	private ListView listview;
	private int status = PROVINCE;
	private final static int PROVINCE=0; //1级省
	private final static int TOWN=1;     //2 市
	private final static int CITY=2;     //3 县
	private ArrayAdapter<String > adapter;
	private ArrayList<String> list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		listview = getListView();
		listview.setOnItemClickListener(this);
		list = GetProvince();
		adapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1, list);
		listview.setAdapter(adapter);
	}
	
	//省
	private ArrayList<String> GetProvince(){
		ArrayList<String> listdataArrayList = new ArrayList<>();
	    SQLiteDatabase db = new DBHelper(getApplicationContext(), "City.db").getReadableDatabase();
		Cursor cursor = db.query("province_table", new String[]{"PROVINCE"}, null, null, null, null, null);
//		if (!cursor.moveToFirst()) {
//			return listdataArrayList;
//		}
		while (cursor.moveToNext()) {
			String item = cursor.getString(cursor.getColumnIndex("PROVINCE"));
			listdataArrayList.add(item);
		}
	    return listdataArrayList;
	}
	
	//示
	private ArrayList<String> GetTown(String p){
		ArrayList<String> listdataArrayList = new ArrayList<>();
		SQLiteDatabase db = new DBHelper(getApplicationContext(), "City.db").getReadableDatabase();
		String where = "PROVINCE='"+p+"'";
		Cursor cursor = db.query("town_table", new String[]{"TOWN"}, where, null, null, null, null);
//		if (!cursor.moveToFirst()) {
//			return listdataArrayList;
//		}
		while (cursor.moveToNext()) {
			String item = cursor.getString(cursor.getColumnIndex("TOWN"));
			listdataArrayList.add(item);
		}
	    return listdataArrayList;
	}
	//县
	private ArrayList<String> GetCity(String t){
		ArrayList<String> listdataArrayList = new ArrayList<>();
		SQLiteDatabase db = new DBHelper(getApplicationContext(), "City.db").getReadableDatabase();
		String where = "TOWN='"+t+"'";
		Cursor cursor = db.query("city_table", new String[]{"CITY"}, where, null, null, null, null);
//		if (!cursor.moveToFirst()) {
//			return listdataArrayList;
//		}
		while (cursor.moveToNext()) {
			String item = cursor.getString(cursor.getColumnIndex("CITY"));
			listdataArrayList.add(item);
		}
	    return listdataArrayList;
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		if (status==PROVINCE) {
			String current_province = list.get(position);
			ArrayList<String> town = GetTown(current_province);
			System.out.println("------------------lenght:"+town.size());
			list.clear();
			list.addAll(town);
			status = TOWN;
			adapter.notifyDataSetChanged();
			return;
		}
		if (status==TOWN) {
			String current_town = list.get(position);
			ArrayList<String> city = GetCity(current_town);
			System.out.println("------------------lenght:"+city.size());
			list.clear();
			list.addAll(city);
			status = CITY;
			adapter.notifyDataSetChanged();
			return;
		}
		if (status==CITY) {
			//Toast.makeText(getApplicationContext(), list.get(position), 0).show();
			Intent intent = new Intent();
			intent.putExtra("city", list.get(position));
			setResult(0, intent);
			finish();
			return;
		}
	}
}
